package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.bean.WheelValueBean;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.event.DeviceItemChangeEvent;
import com.qiantu.phone.event.DeviceTypeChangeEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.ui.dialog.BottomWheelViewDialog;
import com.lxj.xpopup.XPopup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class DeviceSettingChangeType extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;
    private WheelValueBean selectedValue;

    public DeviceSettingChangeType(AppActivity context, DeviceBean deviceBean) {
        this(context, null, deviceBean);
    }

    public DeviceSettingChangeType(AppActivity context, AttributeSet attrs, DeviceBean deviceBean) {
        this(context, attrs, 0, deviceBean);
    }

    public DeviceSettingChangeType(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean);
    }

    public DeviceSettingChangeType(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(R.string.device_protocol_switching);
        setOnClickListener(this::onClick);
    }

    private BottomWheelViewDialog dialog;
    private void showDialog(){
        if (dialog == null){
            dialog = new BottomWheelViewDialog(mContext, getData());
            dialog.setLabel(getResources().getString(R.string.device_protocol_switching));
            dialog.setDialogClickListener(new BottomWheelViewDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(WheelValueBean wheelValue) {
                    dialog.dismiss();
                    selectedValue = wheelValue;
                    changeType();
                }
            });
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(dialog);
        }
        dialog.setCurItem(mDevice.getDeviceType() + "");
        dialog.show();
    }

    private List<WheelValueBean> getData(){
        String type[] = {"human_body_sensor2","infrared_contrast","toxic_gas","vibration_sensing","infrared_curtain","smoke_alarm","trigger_switch","alarm_switch","human_body_sensor_radar","door_contact"};
        int[] name = {R.string.human_body_sensor2,R.string.infrared_contrast,R.string.toxic_gas,R.string.vibration_sensing,R.string.infrared_curtain,R.string.smoke_alarm,R.string.trigger_switch,R.string.alarm_switch,R.string.human_body_sensor_radar,R.string.door_contact};
        List<WheelValueBean> items = new ArrayList<>();
        for (int i=0; i< name.length; i++){
            WheelValueBean bean = new WheelValueBean(getResources().getString(name[i]),type[i]);
            items.add(bean);
        }
        return items;
    }


    private void changeType() {
        String deviceType = selectedValue.getValue();
        LLHttpManager.changeDeviceType(mContext, mDevice.getDeviceSerialNo(), deviceType, new HttpCallback<HttpData<Void>>(mContext){
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                getDeviceData();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                hideDialog();
            }
        });
    }

    private void getDeviceData() {
        LLHttpManager.getDeviceBySerialNo(mContext, mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<DeviceBean>>(mContext){
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onSucceed(HttpData<DeviceBean> result) {
                super.onSucceed(result);
                LLDeviceDBManager.getInstance(getContext()).remove(mDevice);
                LLDeviceDBManager.getInstance(getContext()).put(result.getData());
                EventBus.getDefault().post(new DeviceItemChangeEvent(mDevice.getRoomSerialNo(), mDevice.getDeviceSerialNo(), mDevice.getIsFavourite()));
                EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                EventBus.getDefault().post(new DeviceTypeChangeEvent(mDevice.getDeviceSerialNo()));
                EventBus.getDefault().post(new RefreshDeviceListEvent());
                mContext.toast(R.string.reset_success);
                hideDialog();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                hideDialog();
            }
        });
    }

    private void hideDialog(){
        if(getContext() != null && getContext() instanceof AppActivity){
            ((AppActivity)getContext()).hideDialog();
        }
    }

    @Override
    public void onClick(View v) {
        showDialog();
    }
}
